﻿using M_ApiResult;
using M_Process.API.Appliction.Command;
using M_Process.API.Appliction.Command.ProcessRouteCommand;
using M_Process.API.Appliction.Command.ProductionProcessCommand;
using M_Process.Domain;
using M_Process.Domain.DTO;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace M_Process.API.Controllers
{
    /// <summary>
    /// 工艺信息控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProcessRouteController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        public ProcessRouteController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 反填工艺下的工序
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<ProductionProcess>> BackGong([FromQuery] ProcessProceduresBackfillGoongyiCommand command)
        {
            ApiResult<ProductionProcess> result = new ApiResult<ProductionProcess>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填工序
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<ProcessProcedures>> GOANGxu([FromQuery] ProcessProceduresCommand command)
        {
            ApiResult<ProcessProcedures> result = new ApiResult<ProcessProcedures>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加工艺信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateProcessRoute(ProcessRouteCreateCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "添加工艺信息失败";
                throw;
            }
            return result;

        }
        /// <summary>
        /// 工艺分页显示信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回工艺信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<ProcessRoute>>> GetProcessRouteList([FromQuery]ProcessRouteListCommand command)
        {
            ApiResult<PageDTO<ProcessRoute>> result=new ApiResult<PageDTO<ProcessRoute>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code= -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除工艺信息（单删）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteProcessRoute([FromQuery]ProcessRouteDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await _mediator.Send(command);

            }
            catch (Exception)
            {
                result.Code=-1;
                result.Message = "单独删除工艺信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除工艺信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteAllProcessRoute(ProcessRouteDeleteAllCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "批量删除工艺信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填工艺数据
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult <ProcessRoute>> Handle([FromQuery]ProcessRouteByidCommand command)
        {
            ApiResult <ProcessRoute> result = new ApiResult<ProcessRoute>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填工序信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<ProductionProcess>> ReverseGetByidProductionProcess([FromQuery] ReverseGetByidProcessTasksCommand command)
        {
            ApiResult<ProductionProcess> result = new ApiResult<ProductionProcess>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "反填工序信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改工艺信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateProcessRoute(ProcessRouteUpdateCommand command)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Code= await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code=-1;
                result.Message = "修改工艺信息失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 新增工序信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreadProductionProcess(CreadProductionProcessCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 工序列表分页展示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductionProcessDto<ProductionProcess>>> ShowProductionProcessList([FromQuery] ShowProductionProcessCommand command)
        {
            ApiResult<ProductionProcessDto<ProductionProcess>> result = new ApiResult<ProductionProcessDto<ProductionProcess>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除工序信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteProductionProcesse(DeletedProductionProcessCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 批量删除工序
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDeleteProductionProcess(BatchDeleteProductionProcessCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填工序信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductionProcess>> ReverseFillingProductionProcess([FromQuery] ReverseFillingProductionProcessCommand command)
        {
            ApiResult<ProductionProcess> result = new ApiResult<ProductionProcess>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改工序信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateProductionProcess(UpdateProductionProcessCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
